<template>
	<view>
		<view class="cu-form-group">
			<view class="title">编号</view>
			<input  disabled="true"   :placeholder="item.livestockTagNumber" name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">出生日期</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
			    <view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">{{date}}</view>
			</picker>
		</view>
			<view class="cu-form-group">
			<view class="title">出生地</view>
			<input  v-model="livestock_place"  :placeholder="item.livestockPlace" name="input"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">性别</view>
				<picker @change="GenderPickerChange" :value="index" :range="genderPicker">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex>-1?genderPicker[index]:'请选择性别'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
			<view class="title">体重</view>
			<input  v-model="livestock_weight"  :placeholder="item.livestockWeight" name="input">KG</input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">所在猪栏</view>
				<picker @change="PigstyChange" :value="index3" :range="pistyList">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex3>-1?pistyList[index3]:'请选择猪栏'}}
					</view>
				</picker>
			</view>
			
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
			format: true
			})
			return {
				item:"",
				index3:-1,
				changeIndex3: -1,
				index: -1,
				changeIndex: -1,
				genderPicker: ['公','母'],//性别picker选择列表
				pistyList: [],//猪栏总列表
				date: currentDate,
				pigstyPicker:'',
				livestock_weight:"",
				livestock_place:"",//地点,
				time:''
			}
		},
		onNavigationBarButtonTap() {
			uni.showModal({
				title:"提示",
				content:"你确定要保存此次更改吗?",
				success: (res) => {
					if(res.confirm){
						if(this.index<0){
							uni.showToast({
								title:"请选择性别",
								icon:"none"
							})
							return;
						}
						else if(this.index3<0){
							uni.showToast({
								title:"请选择猪栏",
								icon:"none"
							})
							return;
						}
						this.updateLivestock();
					}
				}
			})
		},
		created() {
			uni.$on('liveUpDate',(res)=>{
				console.log(res)
				this.item = res
			})
		},
		computed: {
		    startDate() {
		        return this.getDate('start');
		    },
		    endDate() {
		        return this.getDate('end');
		    }
		},
		onReady() {
			this.getPigsty()
		},
		methods: {
			//修改猪的信息
			updateLivestock(){
				console.log(this.item)
				uni.request({
					url:this.confige.webUrl+"/livestock/updateLivestock",
					data:{
					livestock_place: this.livestock_place|| this.item.livestockPlace,
						livestock_weight:this.livestock_weight || this.item.livestockWeight,
						livestock_gender:this.genderPicker[this.index] || this.item.livestockGender,
						pigsty_id:this.pigstyPicker[this.index3].pigsty_id || this.item.pigsty_id,
						livestock_birth_date:this.time || this.item.creatDateTime,
						livestock_id:this.item.livestockId 
					},
					method:"POST",
					header:{
						"X-Token":this.User.token
					},
					success: (res) => {
						if(res.data.code==200){
							uni.showToast({
								title:"修改成功"
							});
							uni.switchTab({
								url:"../rear"
							})
						}
					},fail: (err) => {
						console.log(err)
					}
				})
			},
			//日期选择
			bindDateChange: function(e) {
			    this.date = e.target.value
				this.time = e.target.value
				console.log(this.time)
			},
			getDate(type) {
			    const date = new Date();
			    let year = date.getFullYear();
			    let month = date.getMonth() + 1;
			    let day = date.getDate();
			
			    if (type === 'start') {
			        year = year - 60;
			    } else if (type === 'end') {
			        year = year + 2;
			    }
			    month = month > 9 ? month : '0' + month;;
			    day = day > 9 ? day : '0' + day;
			    return `${year}-${month}-${day}`;
			},
			
			//猪栏选择
			PigstyChange: function(e) {
				this.index3 = e.target.value
				this.changeIndex3 = this.index3
			},
			//性别选择器
			GenderPickerChange: function(e) {
				this.index = e.target.value
				this.changeIndex = this.index
				console.log(this.genderPicker[this.index])
			},
			//获取猪栏
			getPigsty(){
				uni.request({
					url:this.confige.webUrl+'/livestock/findAllPigsty',
					data:{
						farmId: this.User.userInfo.farm_id
					},
					header:{
						"X-Token":this.User.token
					},
					success: (res) => {
						this.pigstyPicker = res.data.result;
						console.log(this.pigstyPicker)
						let list = res.data.result;
						var arr = [];
						for(let i=0;i<list.length;i++){
							arr.push(list[i].pigsty_number)
							
						}
						console.log(arr)
							this.pistyList = arr
					}
				})
			},
			
		}
	}
</script>

<style lang="less">
@import "../../../common/global.less";
   @import "../../../common/main.css";
   @import "../../../common/colorui/icon.css";
   @import "../../../common/iconfont.css";
</style>
